package com.hjm.ui.goods;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjm.ui.R;
import com.hjm.ui.goods.adapter.GoodsItemAdapter;
import com.hjm.ui.goods.entity.GoodsItem;

import java.util.List;

public class GoodsItemList extends LinearLayout {
    private RecyclerView homeGoodsRecyclerView;
    List<GoodsItem> goodsList;
    GoodsItemAdapter goodsItemAdapter;
    public GoodsItemList(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    void init(){
        LayoutInflater.from(getContext()).inflate(R.layout.layout_home_item, this, true);
        homeGoodsRecyclerView = findViewById(R.id.recycler_view_home_goods);
    }

    public void setData(List<GoodsItem> goodsList,int row,GoodsItemAdapter.Listener listener){
        Log.d("商品列表组件", "setData: 获取到数据 "+goodsList.size());
        this.goodsList = goodsList;
        if (homeGoodsRecyclerView != null){
            // 两列
            homeGoodsRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), row));
            // 设置不能滑动
            homeGoodsRecyclerView.setNestedScrollingEnabled(false);
            goodsItemAdapter = new GoodsItemAdapter(goodsList,listener,getContext());
            homeGoodsRecyclerView.setAdapter(goodsItemAdapter);
            goodsItemAdapter.notifyDataSetChanged();
        }
    }
}
